package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrdersettingDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import com.itheima.service.SetmealService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImp implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrdersettingDao ordersettingDao;
    @Override
    public void add(int[] checkgroupIds, Setmeal setmeal) {
        setmealDao.add(setmeal);
        Integer id = setmeal.getId();//获取返回的主键
        System.out.println(id);
        //存储中间表
        if(checkgroupIds!=null&&checkgroupIds.length!=0){
            for (int checkgroupId : checkgroupIds) {
                setmealDao.addSetmealAndcheckGroup(id,checkgroupId);
            }
        }

    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String string=null;
        if(queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()!=0){
            string ="%"+queryPageBean.getQueryString()+"%";
        }else{
            string ="%%";
        }
        Page<Setmeal> page = setmealDao.findByPage(string);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findById(int id) {
        List<CheckGroup> checkGroupList = setmealDao.findsetmealAndcheckgroupById(id);
        Setmeal setmeal = setmealDao.findById(id);
        setmeal.setCheckGroups(checkGroupList);

        return setmeal;
    }

    @Override
    public Result save(Map<String, Object> map) throws Exception {
//
//        System.out.println(id);
        String orderDate = (String)map.get("orderDate");
        //判断当前日期是否可以预约
        OrderSetting byDate = ordersettingDao.findByDate(orderDate);
        if(byDate==null){
             return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断预约人数是否已经满了
        String setmealId = (String)map.get("setmealId");
        System.out.println(setmealId);
        int num=setmealDao.isOrder(orderDate);
        if(num==0){
          return new Result(false, MessageConstant.ORDER_FULL);
        }

        //判断当前用户是否为会员用户
        String telephone = (String)map.get("telephone");
        Member member = memberDao.findByPhone(telephone);
        Date date = DateUtils.parseString2Date(orderDate);
        Member member1=null;
        Order orderInfo=null;
        if(member==null){
            member1= new Member();
            //获取用户信息
            String name =(String) map.get("name");
            String sex =(String) map.get("sex");
            String idCard =(String) map.get("idCard");
            member1.setIdCard(idCard);
            member1.setName(name);
            member1.setPhoneNumber(telephone);
            member1.setSex(sex);
            member1.setRegTime(new Date());
            memberDao.saveVip(member1);
            System.out.println(member1.getId());
            //保存信息到预约表
           orderInfo = new Order(member1.getId(),date,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,Integer.parseInt(setmealId));
        }else{
            //判断是否该会员是否预约过
            Integer vipId = member.getId();
           OrderSetting orderSetting =  ordersettingDao.findByIdAndSetmealId(vipId,setmealId);
           if(orderSetting!=null){
               return new Result(false, MessageConstant.HAS_ORDERED);
           }
            orderInfo= new Order(member.getId(),date,Order.ORDERTYPE_WEIXIN,Order.ORDERSTATUS_NO,Integer.parseInt(setmealId));
        }


        //新增预约
        ordersettingDao.saveOrder(orderInfo);
        //修改预约人数
        ordersettingDao.editReservations(orderDate);
        return new Result(true,MessageConstant.ORDER_SUCCESS,orderInfo.getId());
    }
}
